package com.atguigu.demo.customer.controller;

import com.atguigu.demo.customer.bean.Customer;
import com.atguigu.demo.customer.service.CustomerService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController  //@Controller
public class CustomerController {

    @Autowired
    @Qualifier("service2")
    CustomerService customerService ;


    // 读场景
    @GetMapping(value = "/customer" )     //场景： 很多参数，一般是作为查询条件
    public String getCustomer(@RequestParam("name") String name ,@RequestParam(value = "age",required = false,defaultValue = "0") Integer age){
      //0212
        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq("name",name));

        return  "hello world!!!: customers ="+customerList;
    }

    @GetMapping("/customer/{id}.html")  //场景：  单一参数，一般放唯一主键
    public String getCustomerById(@PathVariable("id") String id){
        Customer customer = customerService.getCustomerById(id); //1216
        return  "customer  :  ="+customer.getName();
    }

   //写场景
    @PostMapping(value = "/customer"  )
    public String saveCustomer(@RequestBody Customer customer){
        System.out.println("save customer "+customer);
        //customerService.saveCustomer(customer);
        customerService.saveOrUpdate(customer);
        return "success";

    }



}
